﻿// -----------------------------------------------------------------------
// <copyright file="ItemCollectionViewModel.cs" company="Matthieu Klotz">
// Copyright (c) 2012, Matthieu Klotz
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name of Matthieu Klotz nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// </copyright>
// <summary>A generic view model that contains a collection of item.</summary>
// -----------------------------------------------------------------------

namespace AppFabric.Admin.Client.Core.ViewModel
{
    using System.Collections.ObjectModel;
    using GalaSoft.MvvmLight;

    /// <summary>
    /// A generic view model that contains a collection of item.
    /// </summary>
    /// <typeparam name="TItem">The type of the item.</typeparam>
    public class ItemCollectionViewModel<TItem> : ViewModelBase
    {
        /// <summary>
        /// Collection items.
        /// </summary>
        private ObservableCollection<TItem> items;

        /// <summary>
        /// The current selected item.
        /// </summary>
        private TItem selectedValue;

        /// <summary>
        /// Index of the selected item.
        /// </summary>
        private int selectedIndex = -1;

        /// <summary>
        /// The current selected child.
        /// </summary>
        private int selectedChildIndex = -1;

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>
        /// The items.
        /// </value>
        public ObservableCollection<TItem> Items
        {
            get
            {
                return this.items;
            }

            set
            {
                if (value != this.items)
                {
                    ObservableCollection<TItem> oldValue = this.items;
                    this.items = value;
                    this.RaisePropertyChanged<ObservableCollection<TItem>>("Items", oldValue, value, false);
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected item.
        /// </summary>
        /// <value>
        /// The current selected item.
        /// </value>
        public TItem SelectedValue
        {
            get
            {
                return this.selectedValue;
            }

            set
            {
                if (!value.Equals(this.selectedValue))
                {
                    TItem oldValue = this.selectedValue;
                    this.selectedValue = value;
                    this.RaisePropertyChanged<TItem>("SelectedValue", oldValue, value, false);
                }
            }
        }

        /// <summary>
        /// Gets or sets the index of the selected item.
        /// </summary>
        /// <value>
        /// The index of the selected item.
        /// </value>
        public int SelectedIndex
        {
            get
            {
                return this.selectedIndex;
            }

            set
            {
                if (value != this.selectedIndex)
                {
                    this.selectedIndex = value;
                    this.RaisePropertyChanged("SelectedIndex");
                }
            }
        }

        /// <summary>
        /// Gets or sets the index of the selected child.
        /// </summary>
        /// <value>
        /// The index of the selected child.
        /// </value>
        public int SelectedChildIndex
        {
            get
            {
                return this.selectedChildIndex;
            }

            set
            {
                this.selectedChildIndex = value;
                this.RaisePropertyChanged("SelectedChildIndex");
            }
        }
    }
}